package top.infopub.mgr.hyp.service.impl;


import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.hyp.dao.HyMenuDao;
import top.infopub.mgr.hyp.service.HyMenuService;
import top.infopub.model.hy.Menu;


@Service
public class HyMenuServiceImpl extends GenericServiceImpl<Menu, String> implements HyMenuService {

    @Autowired
    private HyMenuDao menuDao;

    @Override
    public GenericDao<Menu, String> getDao() {
        return menuDao;
    }

    public List<Menu> queryByUserId(String userId) {
        return this.menuDao.selectByUserId(userId);
    }

    @Override
    public boolean isExistName(Menu param) {
        return menuDao.isNameExist(param) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Menu menu, String id) {
        if (id == null) {// 如果id为空，则为添加
            // 先设置父节点不是叶子节点
            Menu menu1 = menuDao.selectByPrimaryKey(menu.getSparentId());
            if (menu1.getBisLeaf() == 1) {
                menu1.setBisLeaf(0);
                menuDao.updateByIdSelective(menu1);
            }
            // 设置节点
            menu.setBisLeaf(1);
            menu.setBisRoot(0);
            menu.setBisDisplay(1);
            menu.setDaddDate(new Date());
        }
        menuDao.insert(menu);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String[] checkboxId) {
        for (String id : checkboxId) {
            menuDao.deleteByPrimaryKey(id);
        }
    }
}